package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.mapper.AddressBookMapper;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "C端-地址簿接口")
@Slf4j
public class AddressBookController {
    @Autowired
    AddressBookService addressBookService;
    /**
     * 新增一个地址
     * @return
     */
    @PostMapping
    public Result add(@RequestBody AddressBook addressBook){
        log.info("新增一个地址{}", addressBook);
        addressBookService.add(addressBook);
        return Result.success();
    }
    /**
     * 查询所有的地址
     */
    @GetMapping("/list")
    public Result listAll(){
        log.info("展示所有的地址");
       List<AddressBook> list = addressBookService.listAll();
        return Result.success(list);
    }
    /**
     * 查询默认地址
     */

    @GetMapping("/default")
    public Result selectDef(){
        log.info("查询默认的地址");
        AddressBook addressBook= addressBookService.selectDef();
        return Result.success(addressBook);

    }
    /**
     * 根据id查询地址
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        log.info("根据id查询地址{}", id);
        AddressBook addressBook= addressBookService.getById(id);
        return Result.success(addressBook);

    }
    /**
     *设置默认的地址
     */
    @PutMapping("/default")
    public Result setDefAddress(@RequestBody AddressBook addressBook){
        log.info("设置默认的地址");
        addressBookService.setDefAddress(addressBook);
        return Result.success();
    }
    /**
     * 更新操作
     */
    @PutMapping
    public Result update(@RequestBody AddressBook addressBook){
        log.info("更新地址{}",addressBook);
        addressBookService.update(addressBook);
        return Result.success();
    }
    /**
     * 删除操作
     */
    public Result delete(Long id){
        log.info("根据id删除{}", id);
        addressBookService.delete(id);
        return Result.success();
    }


}
